/*global define, console */

define(['angular', 'require', 'base'], function (angular, require, base) {
    "use strict";

    var factory = function ($rootScope, $http, $ionicLoading, CommonService, LocalCacheService) {

        var type = 2; //小财迷分类
        var page_size = 20;

        return {
            /**
             * 查询根栏目  fdxqszg 9  otcfdsy 5
             */
            getChannelInfosByRoot: function () {
                return _ajax("/prod/catalog/catalogs", {tree_type: type, catalog_level: 1});
            },
            /**
             * 查询子栏目
             */
            getChannelInfosByParentId: function (channel_id) {
                return _ajax("/prod/catalog/child_catalogs", {parent_catalog_id: channel_id});
            },
            getChannelInfosByParentMark: function (catalog_mark) {
                return _ajax("/prod/catalog/child_catalogs", {catalog_mark: catalog_mark});
            },
            /**
             * 查询分类详情
             */
            getChannelInfo: function (channel_id) {
                return _ajax("/prod/catalog/catalog_detail", {catalog_id: channel_id});
            },
            /**
             * 查询父类目下的产品
             */
            getProductsByParent: function (channel_id, page_number, tree_type, price_increase_qry_type) {
                return _ajax("/prod/list/parentcatalog_prods", {
                    parent_catalog_id: channel_id,
                    orderRuleType: 1,
                    page_size: page_size,
                    page_number: page_number
                });
            },
            /**
             * 查询产品
             */
            getProductsByChannelId: function (channel_id, page_number, tree_type, price_increase_qry_type) {
                return _ajax("/prod/list/prods", {
                    catalog_id: channel_id,
                    tree_type: tree_type ? tree_type : type,
                    page_size: page_size,
                    page_number: page_number,
                    price_increase_qry_type: price_increase_qry_type ? price_increase_qry_type : null
                });
            },
            /**
             * 查询栏目下的otc产品
             */
            getOtcProductsByChannelId: function (channel_id, page_number,tree_type) {
                return _ajax("/prod/list/otc_prods", {
                    catalog_id: channel_id,
                    tree_type: tree_type ? tree_type : type,
                    page_size: page_size,
                    page_number: page_number
                });
            },
            /**
             * 批量产品概要 产品代码查询
             */
            getProdBriefs: function (prod_codes) {
                return _ajax("/prod/list/prods", {prod_codes: prod_codes});
            },
            getOtcProducts: function (prod_codes) {
                return _ajax("/prod/list/otc_prods", {prod_codes: prod_codes});
            },
            /**
             * 查询指数收益
             */
            getIndexProfit: function (indexCodes) {
                return _ajax("/prod/history/index_profit", {index_code: indexCodes});
            },
            /**
             * 查询指数历史数据
             */
            getHistoryIndex: function (indexCode, begin_date, end_date) {
                if (begin_date && begin_date.length == 8) {
                    return _ajax("/prod/history/index", {
                        index_code: indexCode,
                        begin_date: begin_date,
                        end_date: end_date
                    });
                } else {
                    return _ajax("/prod/history/index", {index_code: indexCode, count: begin_date});
                }
            },
            /**
             * 查询子栏目产品
             */
            getChildCatalogProds: function (channel_id, page_number) {
                return _ajax("/prod/list/childcatalog_prods", {
                    parent_catalog_id: channel_id,
                    tree_type: type,
                    page_size: page_size,
                    page_number: page_number
                });
            },
            /**
             * 获取OTC产品概况
             */
            getOTCGeneral: function (prod_code) {
                return _ajax("/prod/single/otc", {prod_code: prod_code});
            },
            /**
             * 获取产品概况
             */
            getFundGeneral: function (prod_code) {
                return _ajax("/prod/single/prod", {prod_code: prod_code});
            },
            /**
             * 获取产品费率数据
             */
            getProdRate: function (prod_id) {
                return _ajax("/prod/single/prod_rate", {prod_id: prod_id});
            },
            /**
             * 查询OTC明细
             */
            getOTCDetail: function (prod_code) {
                return _ajax("/prod/single/otc_detail", {prod_code: prod_code});
            },
            /**
             * 查询基金明细
             */
            getContentDetail: function (prod_code) {
                return _ajax("/prod/single/prod_detail", {prod_code: prod_code});
            },
            /**
             * 查询货基七日年化
             */
            getHistoryYield: function (prod_id, count) {
                return _ajax("/prod/history/yield", {prod_id: prod_id, count: count});
            },
            /**
             * 基金净值，沪深300指数历史数据
             */
            getComparisonData: function (prod_id, count) {
                return _ajax("/prod/history/csi300_nav", {prod_id: prod_id, count: count});
            },
            /**
             * 查询基金历史净值数据
             */
            getHistoryNav: function (prod_id, count) {
                return _ajax("/prod/history/nav", {prod_id: prod_id, count: count});
            },
            /**
             * 获取推荐的关注列表
             */
            getOthersCare: function () {
                return _ajaxNoCache("/prod/list/others_care");
            },
            /**
             * 短债产品可赎回查询
             */
            getShortTermFund: function (prod_code, trans_account) {
                return _ajax("/prod/single/short_sell", {fund_code: prod_code, transaction_accountid: trans_account});
            },
            /**
             * 根据输入日期查询相关收益日期
             */
            getProfitDate: function (prod_code, entrust_date) {
                return _ajax("/prod/single/profit_date", {prod_code: prod_code, entrust_date: entrust_date});
            },
            /**
             * 获取产品文件列表
             */
            getProdFiles: function (prod_id) {
                return _ajax("/prod/single/prod_files", {prod_id: prod_id});
            },
            /**
             * 获取产品文件详情
             */
            getProdFileContent: function (file_id) {
                return _ajax("/prod/single/prod_file/detail", {file_id: file_id});
            },
            /**
             * 根据主题标示 获取主题下的产品列表
             */
            getThemeDetail: function (theme_mark) {
                return _ajax("/prod/single/prod_theme/detail", {theme_mark: theme_mark});
            },
            /**
             * 根据主题标示 获取主题下的产品列表
             */
            getProdByTheme: function (theme_mark, relate_type, order_rule) {
                return _ajax("/prod/single/prod_theme/prods", {
                    theme_mark: theme_mark,
                    relate_type: relate_type,
                    order_rule: order_rule
                });
            },
            getProductsByTheme: function (theme_mark, relate_type, order_rule) {
                return _ajax("/prod/single/prod_theme_otc/prods", {
                    theme_mark: theme_mark,
                    relate_type: relate_type,
                    order_rule: order_rule
                });
            },
            searchProds: function (conditions, page_number) {
                return _ajax("/prod/list/search_prods", {
                    conditions: encodeURI(conditions),
                    page_size: page_size,
                    page_number: page_number
                });
            },
            /**
             * 获取资金精选产品列表
             */
            getProductsList: function () {
                return _ajax("/prod/list/fund", null);
            }
        };

        /**
         * 内部调用ajax方法
         * @param url
         * @private
         */
        function _ajax(url, data) {
            var params = {};
            params.url = url;
            params.type = "get";
            params.cache = true;
            if (data) {
                params.data = data;
            }
            return CommonService.ajax(params);
        }

        function _ajaxNoCache(url, data) {
            var params = {};
            params.url = url;
            params.type = "get";
            params.cache = false;
            if (data) {
                params.data = data;
            }
            return CommonService.ajax(params);
        }
    };

    factory.$inject = ['$rootScope', '$http', '$ionicLoading', 'CommonService', 'LocalCacheService'];
    return factory;
});